﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace RCREEE.Web
{
    public partial class Search : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Session["LanguageCode"] = 1033;
        }

        protected void ddlSearchFor_SelectedIndexChanged(object sender, EventArgs e)
        {
            mlvSearchTypes.ActiveViewIndex = Convert.ToInt32(ddlSearchFor.SelectedValue) - 1;

            if (ddlSearchFor.SelectedValue == "0")
                btnSearch.Visible = false;
            else
                btnSearch.Visible = true;
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Library.Search _search = new Library.Search();
            string url = "";
            DataTable dtSearchResults = new DataTable();

            switch (mlvSearchTypes.ActiveViewIndex)
            {
                case 0: // Search Projects
                    {
                        if (ddlProjectRecord.SelectedValue != "0" && ddlProjectRecord.SelectedValue != "")
                        {
                            // Redirect to the selected project
                            url = _search.GetRecordLink("Project", Convert.ToInt32(ddlProjectRecord.SelectedValue));
                            if (url != "")
                                Response.Redirect(url);
                        }
                        else if ((ddlCountries.SelectedValue != "0" && ddlCountries.SelectedValue != "") 
                            && (ddlProjectGroup.SelectedValue == "0" || ddlProjectGroup.SelectedValue == "")
                            && (ddlProjectType.SelectedValue == "0" || ddlProjectType.SelectedValue == ""))
                        {
                            // Redirect to the selected Country
                            url = "Country.aspx?C=" + ddlCountries.SelectedValue;
                            Response.Redirect(url);
                        }
                        else
                        {
                            // Fill Search Panel with details according to countries & entities DDLs
                            dtSearchResults = _search.SearchProjects(Convert.ToInt32(ddlCountries.SelectedValue),
                                                                     Convert.ToInt32(ddlProjectGroup.SelectedValue),
                                                                     Convert.ToInt32(ddlProjectType.SelectedValue),
                                                                     Convert.ToInt32(Session["languageCode"]));
                            if (dtSearchResults != null && dtSearchResults.Rows.Count > 0)
                            {
                                dlSearchResults.DataSource = dtSearchResults;
                                dlSearchResults.DataBind();
                                lblNoDataAvailable.Visible = false;
                            }
                            else
                                lblNoDataAvailable.Visible = true;
                        }

                        break;
                    }
                case 1: // Search Contacts
                    {
                        if (ddlContactRecord.SelectedValue != "0" && ddlContactRecord.SelectedValue != "")
                        {
                            // Redirect to the selected record Id
                            url = _search.GetRecordLink("Contact", Convert.ToInt32(ddlContactRecord.SelectedValue));
                            if (url != "")
                                Response.Redirect(url);
                        }
                        else if ((ddlContactEntities.SelectedValue == "0" || ddlContactEntities.SelectedValue == "") && (ddlCountries.SelectedValue != "0" && ddlCountries.SelectedValue != ""))
                        {
                            // Redirect to the selected Country
                            url = "Country.aspx?C=" + ddlCountries.SelectedValue;
                            Response.Redirect(url);
                        }
                        else
                        {
                            // Fill Search Panel with details according to countries & entities DDLs
                            dtSearchResults = _search.SearchContacts(Convert.ToInt32(ddlCountries.SelectedValue),
                                                                     Convert.ToInt32(ddlContactEntities.SelectedValue),
                                                                     Convert.ToInt32(Session["languageCode"]));
                            if (dtSearchResults != null && dtSearchResults.Rows.Count > 0)
                            {
                                dlSearchResults.DataSource = dtSearchResults;
                                dlSearchResults.DataBind();
                                lblNoDataAvailable.Visible = false;
                            }
                            else
                                lblNoDataAvailable.Visible = true;
                        }
                        break;
                        
                    }
                case 2: // Search Items & Cadres
                    {
                        if ((ddlCountries.SelectedValue != "0" && ddlCountries.SelectedValue != "") && txtKeyWord.Text == "")
                        {
                            // Redirect to the selected Country
                            url = "Country.aspx?C=" + ddlCountries.SelectedValue;
                            Response.Redirect(url);
                        }
                        else
                        {
                            dtSearchResults = _search.SearchItems_Cadres(Convert.ToInt32(Session["LanguageCode"]), Convert.ToInt32(ddlCountries.SelectedValue), txtKeyWord.Text);

                            if (dtSearchResults != null && dtSearchResults.Rows.Count > 0)
                            {
                                dlSearchResults.DataSource = dtSearchResults;
                                dlSearchResults.DataBind();
                                lblNoDataAvailable.Visible = false;
                            }
                            else
                                lblNoDataAvailable.Visible = true;
                        }

                        break;
                    }
            }
        }

        
    }
}